﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphicalSVN.GraphicSource.Dto;
using System.Threading;

namespace GraphicalSVN.GraphicSource
{
    public class GraphicalSourceStub : IGraphicalSource
    {
        private readonly GeneratorInfo gi = null;
        private SVNFrameData frameData = new SVNFrameData();
        private SVNFrameData returnFrameData = new SVNFrameData();
        private int frames = 0;
        public GraphicalSourceStub(GeneratorInfo gi)
        {
            this.gi = gi;
            frameData = new SVNFrameData()
            {
                FrameNumber = 0,
                RevisionNumber = 1,
                RevisionDate = DateTime.Now,
            };
            frameData.Items.Add(new SVNObject() { Action = SharpSvn.SvnChangeAction.Add, IsDirectory = true, ObjectPath = "/", ParentDirectory = "/", DoesExist = true });
            frameData.Items.Add(new SVNObject() { Action = SharpSvn.SvnChangeAction.Add, IsDirectory = true, ObjectPath = "/One/", ParentDirectory = "/", DoesExist = true });
            frameData.Items.Add(new SVNObject() { Action = SharpSvn.SvnChangeAction.Add, IsDirectory = true, ObjectPath = "/One/One/", ParentDirectory = "/One/", DoesExist = true });
            frameData.Items.Add(new SVNObject() { Action = SharpSvn.SvnChangeAction.Add, IsDirectory = true, ObjectPath = "/One/Two/", ParentDirectory = "/One/", DoesExist = true });
            frameData.Items.Add(new SVNObject() { Action = SharpSvn.SvnChangeAction.Add, IsDirectory = true, ObjectPath = "/One/Three/", ParentDirectory = "/One/", DoesExist = true });
            frameData.Items.Add(new SVNObject() { Action = SharpSvn.SvnChangeAction.Add, IsDirectory = true, ObjectPath = "/One/Three/One/", ParentDirectory = "/One/Three/", DoesExist = true });
            returnFrameData = new SVNFrameData()
                {
                    FrameNumber = 0,
                    RevisionNumber = 1,
                    RevisionDate = DateTime.Now
                };
            returnFrameData.Items.Clear();
            returnFrameData.Items.Add(frameData.Items.First());
        }

        public void Generate()
        {

        }

        public SVNFrameData Update(TimeSpan span)
        {
            lock (this)
            {
                Interlocked.Increment(ref frames);
                if (frames % 20 == 0)
                {
                    int currentItem = frames / 20;
                    if (currentItem < frameData.Items.Count)
                    {
                        returnFrameData.Items.Add(frameData.Items[currentItem]);
                    }
                    if (currentItem == 10)
                    {
                        returnFrameData.Items.RemoveAt(4);
                    }
                }
                return returnFrameData;
            }
        }
    }
}
